package com.glory.wxelectricitypda.activity;


import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.UnpackagedAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.BoxLabelReviewBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class TraceabilityCodeReview extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.codeRecycler)
    RecyclerView codeRecycler;
    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.checkpackLotid)
    FreeUI_ClearEditText checkpackLotid;
    @BindView(R.id.checkShipLabel)
    FreeUI_ClearEditText checkShipLabel;
    public UnpackagedAdapter mUnpackagedAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_traceability_code_review;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("客户追溯码审核");

        codeRecycler.setLayoutManager(new LinearLayoutManager(TraceabilityCodeReview.this));
        codeRecycler.setItemAnimator(new DefaultItemAnimator());
        codeRecycler.addItemDecoration(new DividerItemDecoration(TraceabilityCodeReview.this, 1));
        mUnpackagedAdapter = new UnpackagedAdapter(null);
        mUnpackagedAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mUnpackagedAdapter.isFirstOnly(true);
        codeRecycler.setAdapter(mUnpackagedAdapter);
    }

    @Override
    protected void initData() {

        initEdit(checkShipLabel, s->{

            if (etUser.getText().toString().isEmpty()) {
                showToast("请输入工号", false);
                return;
            }
            if (checkpackLotid.getText().toString().isEmpty()) {
                showToast("请扫描箱号", false);
                return;
            }

            queryTraceabilityCodeReview();
        });
    }

    private void queryTraceabilityCodeReview(){

        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "APPROVESHIPLABEL");
            map.put("CHECKPACKLOTID", checkpackLotid.getText().toString());
            map.put("CHECKSHIPLABEL", checkShipLabel.getText().toString());
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.LABELCHECK");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<BoxLabelReviewBean>>(
                new TypeToken<ResponseBean<BoxLabelReviewBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<BoxLabelReviewBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    boolean type = false;
                    for (BoxLabelReviewBean.XPACKAGEDETAILDTO bean: mUnpackagedAdapter.getData()) {
                        if (bean.getPACKLOTID().equals(response.body().getBody().getXPACKAGEDETAIL().getPACKLOTID())){
                            type = true;
                        }
                    }
                    if (type){
                        showToast("该箱号以审核",false);
                        checkpackLotid.setText("");
                        checkShipLabel.setText("");
                        return;
                    }
                    mUnpackagedAdapter.addData(response.body().getBody().getXPACKAGEDETAIL());
                }else {
                    checkpackLotid.setText("");
                    checkShipLabel.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<BoxLabelReviewBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LABELCHECK");
            }
        });


    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header",llMain);
    }
}